<template>
  <div class="login">
    <div class="login_box">
      <!--  顶部logo区域 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!--  表单区域 -->

      <el-form  class="login_form"    :rules='loingFormRules' :model='loginForm'  ref="login_form">
        <el-form-item prop="username">
          <i class="el-icon-user-solid"></i>
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <i class="el-icon-view"></i>
          <el-input v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item  class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
    data(){
        return {
           loginForm :{
               username:'',
               password:''
           },
           // 校验规则
           loingFormRules:{
               username: [
             { required: true, message: '请输入用户名', trigger: 'blur' },
             { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
             ],
             password:[
                 { required: true, message: '请输入密码', trigger: 'blur' },
             ]
           }
            
           
        }

    },
    methods:{
        // 登录的事件
        login(){
            console.log(this.loginForm)
            console.log(this.$refs)
            this.$refs.login_form.validate( async valid=>{
                // 返回的是布尔值
                console.log(valid)
                if(!valid){
                    return this.$message.error('不能为空');
                }
                 //   校验通过之后  发送数据请求  
                 //   发送请求使用axios 
                 console.log(this.$http)
                 const {data} = await this.$http.post('/login',this.loginForm)
                 console.log(data)
                //  console.log(data.meta)
                if(data.meta.status!=200){
                    return this.$message.error(data.meta.msg)
                }
                  //  说明登录成功
                  //  token 进行身份认证 
                  const token = data.data.token
                  console.log(token)
                  localStorage.setItem('token',token)
                   this.$message({
                      message: '恭喜你，登录成功',
                      type: 'success'
                    });
                    this.$router.push('/home')
            })
        }
    }
}
</script>

<style scoped   lang="less">
.login {
  /*  百分比相对于父元素说的   */
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 4px;
    padding: 0 20px;
    .logo {
      position: absolute;
      left: 50%;
      margin-left: -76px;
      top: -76px;
      width: 152px;
      height: 152px;
      background-color: #fff;
      padding: 10px;
      border-radius: 50%;
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
      img {
        width: 100%;
        height: 100%;
        background-color: #eee;
        border-radius: 50%;
      }
    }
  }

  .login_form {
    position: absolute;
    bottom: 20px;
    width: 410px;
    .btns {
        display: flex;
        justify-content: flex-end;
    }
    i {
        position: absolute;
        top: 12px;
        left: 12px;
        z-index: 9;
    }
  }
}
</style>
<style>
    .login .el-input__inner{
        padding: 0 30px;
    }
</style>
